package cn.yuyiling.jelly.qa.service.impl;

import cn.yuyiling.jelly.qa.api.CommentService;
import cn.yuyiling.jelly.qa.mongodb.entity.Comment;
import cn.yuyiling.jelly.qa.mongodb.repository.AnswerRepository;
import cn.yuyiling.jelly.qa.mongodb.repository.CommentRepository;
import cn.yuyiling.jelly.qa.mongodb.repository.QuestionRepository;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {
    private static final Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private AnswerRepository answerRepository;
    @Autowired
    private QuestionRepository questionRepository;

    @Override
    public Comment createCommentWithQuestion(String questionId, Comment comment) {
        return questionRepository.findById(questionId)
                .map(question -> {
                    comment.setParentId(questionId);
                    comment.setPath(question.getPath() + questionId + "/");
                    comment.setDepth(question.getDepth() + 1);
                    comment.setCreatedAt(new Date());
                    return commentRepository.insert(comment);
                })
                .orElseThrow(() -> new RuntimeException("Question not found"));
    }

    @Override
    public Comment createCommentWithAnswer(String answerId, Comment comment) {
        return answerRepository.findById(answerId)
                .map(answer -> {
                    comment.setParentId(answerId);
                    comment.setPath(answer.getPath() + answerId + "/");
                    comment.setDepth(answer.getDepth() + 1);
                    comment.setCreatedAt(new Date());
                    return commentRepository.insert(comment);
                })
                .orElseThrow(() -> new RuntimeException("Answer not found"));
    }

    @Override
    public Comment likeComment(String id) {
        return commentRepository.findById(id)
                .map(comment -> {
                    comment.setLikes(comment.getLikes() + 1);
                    return commentRepository.save(comment);
                })
                .orElseThrow(() -> new RuntimeException("Comment not found"));
    }

    @Override
    public Comment dislikeComment(String id) {
        return commentRepository.findById(id)
                .map(comment -> {
                    comment.setDislikes(comment.getDislikes() + 1);
                    return commentRepository.save(comment);
                })
                .orElseThrow(() -> new RuntimeException("Comment not found"));
    }

    @Override
    public void deleteCommentsByQuestionId(String questionId) {
        // 查找所有path中包含questionId的评论
        List<Comment> comments = commentRepository.findByPathContaining("/" + questionId + "/");
        // 批量删除这些评论
        commentRepository.deleteAll(comments);
    }

    @Override
    public void deleteCommentsByAnswerId(String answerId) {
        // 查找所有path中包含answerId的评论
        List<Comment> comments = commentRepository.findByPathContaining("/" + answerId + "/");
        // 批量删除这些评论
        commentRepository.deleteAll(comments);
    }

    @Override
    public void deleteCommentAndRelatedData(String commentId) {
        commentRepository.deleteById(commentId);
    }

    @Override
    public List<Comment> getCommentsByQuestionId(String questionId) {
        return commentRepository.findByPathContaining("/" + questionId + "/");
    }
}
